
package ch.hearc.securiteSysteme.serie2.ex2ChallengeResponse.server;

import java.io.ObjectInputStream;

/**
 * Cette class permet la réception des sms sur le server.
 * @author Raphaël Capocasale
 *
 */
public class Reception implements Runnable
	{
	
	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	//tools
	private ObjectInputStream m_objectInputStream;
	private Object m_message;
	private OnMessageListener_I m_onMessageListener;
	private boolean m_isReceptionRunning;
	
	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public Reception()
		{
		m_onMessageListener = null;
		m_isReceptionRunning = true;
		m_message = null;
		m_objectInputStream = null;
		}
	
	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	
	public void run()
		{
		while(m_isReceptionRunning)
			{
			if (m_objectInputStream != null)
				{
				try
					{
					m_message = m_objectInputStream.readObject();
					this.onProgressUpdate();
					}
				catch (Exception e)
					{
					}
				}
			}
		}
	
	public void cancel()
		{
		m_isReceptionRunning = false;
		}
	
	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/
	public void setInObject(ObjectInputStream inObject)
		{
		m_objectInputStream = inObject;
		}
	
	public void setOnMessageListener(OnMessageListener_I listener)
		{
		m_onMessageListener = listener;
		}
	
	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	private void onProgressUpdate()
		{
		new Thread(new Runnable()
			{
				
				public void run()
					{
					if (m_message != null)
						{
						m_onMessageListener.onMessageReceived(m_message);
						}
					}
			}).start();
		}
	}
